//
//  UIViewController+STPresent.m
//  SportHome
//
//  Created by stoneobs on 16/11/25.
//  Copyright © 2016年 stoneobs. All rights reserved.
//

#import "UIViewController+STPresent.h"
#import <objc/runtime.h>
static char *STImageVCChar = "STImageVC";
static char *imgePickerBlockChar = "imgePickerBlock";
@implementation UIViewController (STPresent)
#pragma mark --Get and Set
- (void)setSTImageVC:(UIImagePickerController *)STImageVC
{

     objc_setAssociatedObject(self, &STImageVCChar, STImageVC, OBJC_ASSOCIATION_RETAIN);
}
- (UIImagePickerController *)STImageVC
{
 
    return objc_getAssociatedObject(self, &STImageVCChar);
}
- (void)setImgePickerBlock:(IMAGEPICKER)imgePickerBlock
{
    objc_setAssociatedObject(self, &imgePickerBlockChar, imgePickerBlock,OBJC_ASSOCIATION_COPY);

}
- (IMAGEPICKER)imgePickerBlock
{
     return objc_getAssociatedObject(self, &imgePickerBlockChar);

}
#pragma mark --Alert和Actionsheet
- (void)showAlert:(NSString*)message
{
    UIAlertController * art =[UIAlertController alertControllerWithTitle:@"注意！" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancel =[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
    [art addAction:cancel];
    [self presentViewController:art animated:YES completion:nil];
}
- (void)showAlert:(NSString *)message andWithBlock:(ALERTBLOCK)finsh
{
    UIAlertController * vc =[UIAlertController alertControllerWithTitle:@"请注意" message:message preferredStyle:UIAlertControllerStyleAlert];
    [vc addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(@"确认");
        }
    }]];
    [self presentViewController:vc animated:YES completion:nil];
    
}
- (void)showAlertCancelAndConfirm:(NSString*)message andWithBlock:(ALERTBLOCK)finsh
{
    UIAlertController * vc =[UIAlertController alertControllerWithTitle:@"请注意" message:message preferredStyle:UIAlertControllerStyleAlert];
    [vc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(@"取消");
        }
    }]];
    [vc addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(@"确认");
        }
    }]];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
    
}
- (void)showAlertCustomTitleOne:(NSString *)one Two:(NSString*)two  message:(NSString*)message andWithBlock:(ALERTBLOCK)finsh
{
    UIAlertController * vc =[UIAlertController alertControllerWithTitle:@"请注意" message:message preferredStyle:UIAlertControllerStyleAlert];
    [vc addAction:[UIAlertAction actionWithTitle:one style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(one);
        }
    }]];
    [vc addAction:[UIAlertAction actionWithTitle:two style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(two);
        }
    }]];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
}
- (void)showActionSheet:(NSArray<NSString *> *)strArray andWithBlock:(ACTIONBLOCK)test
{
    
    UIAlertController * sheet =[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (int i=0; i<strArray.count; i++) {
        UIAlertAction * action =[UIAlertAction actionWithTitle:strArray[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (test) {
                test(i);
            }
            
            
        }];
        [sheet addAction:action];
    }
    
    [self presentViewController:sheet animated:YES completion:^{
        
    }];
}
#pragma mark ----默认image picker 和他的delegate
-(void)showDefultImagePicker:(IMAGEPICKER)pickerBlock
{
    UIImagePickerController * vc =[[UIImagePickerController alloc]init];
    vc.allowsEditing=YES;
    vc.delegate=self;
    self.STImageVC = vc;
    UIAlertController * sheet =[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * camer =[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (!TARGET_OS_SIMULATOR) {
            self.STImageVC.sourceType=UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self.STImageVC animated:YES completion:nil];
        }
        
    }];
    [sheet addAction:camer];
    UIAlertAction * photo =[UIAlertAction actionWithTitle:@"从相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.STImageVC.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:self.STImageVC animated:YES completion:nil];
    }];
    [sheet addAction:photo];
    UIAlertAction * cancle =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [sheet addAction:cancle];
    
    [self presentViewController:sheet animated:YES completion:nil];
    if (pickerBlock) {
        self.imgePickerBlock=pickerBlock;
    }
    
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage *   image=[info objectForKey:@"UIImagePickerControllerEditedImage"];
        if ( self.imgePickerBlock) {
             self.imgePickerBlock(image);
        }
    }];
}




@end
